#!/usr/bin/env bash

# Run from your JRuby home directory....More smarts needed here.
#
# PATH=$PATH:<path to jay executable> tool/generate_parser
#
# jay is available from https://github.com/jruby/jay
#
# BUILD_DEBUG env set to anything will not remove the parse.y files
# generated so you can look at those files (line: comments in final
# source point back at these files).

###### Change these to tastes ######
JAY=jay
RUBY=${RUBY:=ruby}
PARSER_BASE=RubyParser
YYTABLE_PREFIX=
DEBUG=true
###### Do not change below ######

if [ "$1" != "" ]; then
  PARSER_BASE=$1
  shift
fi

if [ "$1" != "" ]; then
  YYTABLE_PREFIX=$1
fi

if [ "$DEBUG" != "" ]; then
  DEBUG_FLAG=-t
  # Nonesense...my script-fu is weak
  DEBUG_STRIP="xdyhbk"
else
  DEBUG_FLAG=
  DEBUG_STRIP="^//t"
fi

echo "Generating Parser '$PARSER_BASE' w/ YYTable prefix of '$YYTABLE_PREFIX'"

JAVA_SRC=core/src/main/java
PROJECT_ROOT=../../../../../../..
TOOL_DIR=${PROJECT_ROOT}/tool
PARSER_DIR=$JAVA_SRC/org/jruby/parser
RIPPER_DIR=${PROJECT_ROOT}/$JAVA_SRC/org/jruby/ext/ripper

pushd $PARSER_DIR

if [ "$SKIP_PARSER" == "" ]; then
    echo "processing parser"
    $RUBY $TOOL_DIR/preprocess_parser.rb < $PARSER_BASE.y > parse.y

    # Generate grammar as intermediate file
    echo "generating parser using jay"
    $JAY $DEBUG_FLAG parse.y < skeleton.parser | grep -v $DEBUG_STRIP > $PARSER_BASE.out

    echo "patching parser"
    $RUBY $TOOL_DIR/patch_parser.rb $PARSER_BASE.out $YYTABLE_PREFIX $PARSER_BASE.y > $PARSER_BASE.out2

    echo "optimizing parser"
    $RUBY $TOOL_DIR/optimize_parser.rb $PARSER_BASE.out2 $YYTABLE_PREFIX > $PARSER_BASE.java

    rm -f $PARSER_BASE.out $PARSER_BASE.out2

    if [ "$BUILD_DEBUG" == "" ]; then
        rm -f parse.y 
    fi
fi

if [ "$SKIP_RIPPER" == "" ]; then
    echo "processing ripper"
    $RUBY $TOOL_DIR/preprocess_parser.rb ripper < $PARSER_BASE.y  > $RIPPER_DIR/ripper_parse.y

    cd $RIPPER_DIR
    echo "in ripper dir ${PWD}"

    echo "applying ripper DSL"
    $RUBY ../$TOOL_DIR/preproc_ripper.rb ripper_parse.y > ripper_$PARSER_BASE.out

    echo "generating ripper using jay"
    $JAY $DEBUG_FLAG ripper_$PARSER_BASE.out < ../../parser/skeleton.parser | grep -v $DEBUG_STR#IP > ripper_$PARSER_BASE.out2

    echo "patching ripper"
    $RUBY ../$TOOL_DIR/patch_parser.rb ripper_$PARSER_BASE.out2 $YYTABLE_PREFIX ripper_$PARSER_BASE.out > ripper_$PARSER_BASE.out3

    echo "optimizing ripper"
    $RUBY ../$TOOL_DIR/optimize_parser.rb ripper_$PARSER_BASE.out3 $YYTABLE_PREFIX > RipperParser.java

    if [ "$BUILD_DEBUG" == "" ]; then
        rm -f ripper_$PARSER_BASE.out ripper_$PARSER_BASE.out2 ripper_$PARSER_BASE.out3
        rm -f ripper_parse.y
    fi
fi

popd
